package ${package.Controller};

import com.xgry.bpm.common.utils.Result;;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import com.xgry.bpm.api.business.${package.ModuleName}.service.${table.serviceName};
import com.xgry.bpm.api.business.${package.ModuleName}.entity. ${entity};


/**
 * <p>
    * ${table.comment!} 前端控制器
    * </p>
 *
 * @author ${author}
 * @since ${date}
 * @version v1.0
 */
<#if restControllerStyle>
@Api(tags = {"${table.comment!}"})
@RestController
<#else>
@RestController
</#if>
@RequestMapping("<#if package.ModuleName??></#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
    <#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
    <#else>
public class ${table.controllerName} {
    </#if>

    @Autowired
    private ${table.serviceName} ${(table.serviceName?substring(1))?uncap_first};
    /**
     * 查询分页数据
     */
    @ApiOperation(value = "查询分页数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageIndex", value = "查询第几页", paramType = "query", required = true, dataType = "int"),
        @ApiImplicitParam(name = "pageSize", value = "每页多少条", paramType = "query", required = true, dataType = "int"),
        @ApiImplicitParam(name = "${entity?uncap_first}", value = "客户查询参数", paramType = "query", required = false, dataType = "object")
    })
    @RequestMapping(value = "/get${entity}ListForPage", method= RequestMethod.POST)
    public Result get${entity}ListForPage(@RequestParam(name = "pageIndex", defaultValue = "1") int pageIndex,@RequestParam(name = "pageSize", defaultValue = "5") int pageSize , ${entity} ${entity?uncap_first}){
        return Result.convertIpageToSuccess(${(table.serviceName?substring(1))?uncap_first}.page(new Page<>(pageIndex,pageSize),new QueryWrapper<${entity}>(){{
            this.orderByDesc("pk_id");
            this.setEntity(${entity?uncap_first});
        }}));
    }


    /**
    * 查询list数据
    */
    @ApiOperation(value = "查询list数据")
    @ApiImplicitParam(name = "查询数据", value = "查询数据", paramType = "query", required = false, dataType = "object")
    @RequestMapping(value = "/get${entity}List", method= RequestMethod.POST)
    public Result get${entity}List(${entity} ${entity?uncap_first}){
        return Result.success(HttpStatus.OK.getReasonPhrase(),${(table.serviceName?substring(1))?uncap_first}.list(new QueryWrapper<${entity}>(){{
            this.orderByAsc("pk_id");
            this.setEntity(${entity?uncap_first});
        }}));
    }



    /**
     * 新增
     */
    @ApiOperation(value = "新增数据")
    @ApiImplicitParam(name = "${entity?uncap_first}json", value = "新增数据json", paramType = "query", required = true, dataType = "String")
    @RequestMapping(value = "/add${entity}", method = RequestMethod.POST)
    public Result add${entity}(@RequestBody ${entity} ${entity?uncap_first}){

        if (null == ${entity?uncap_first}) {
            return Result.fail("Obj is null!");
        }

        if (${(table.serviceName?substring(1))?uncap_first}.save(${entity?uncap_first})) {
            return Result.success();
        }

        return Result.fail();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除数据")
    @ApiImplicitParam(name="用户ids",value = "用户主键id Array",paramType = "delete",required = true,dataType = "int")
    @RequestMapping(value = "/delete${entity}ByIds", method= RequestMethod.POST)
    public Result delete${entity}(@RequestParam("ids") List<Integer> ids){
        if (CollectionUtils.isEmpty(ids)) {
            return Result.fail("ids is null!");
        }

        if (${(table.serviceName?substring(1))?uncap_first}.removeByIds(ids)) {
            return Result.success();
        }

        return Result.fail();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新数据")
    @ApiImplicitParam(name="用户ids",value = "用户主键id Array",paramType = "update",required = true,dataType = "object")
    @RequestMapping(value = "/update${entity}ById", method = RequestMethod.POST)
    public Result update(@RequestBody ${entity} ${entity?uncap_first}){
        if (null == ${entity?uncap_first} ||  0 == ${entity?uncap_first}.getPkId().intValue()) {
            return Result.fail("obj is null!");
        }

        if (${(table.serviceName?substring(1))?uncap_first}.updateById(${entity?uncap_first})) {
            return Result.success();
        }

        return Result.fail();
     }

}
</#if>